package topofstack.networkserverutils;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity {

	public static final String KEY_LAT = "KEY_LAT";
	public static final String KEY_LON = "KEY_LON";

	private MapView map;

	private MyLocationOverlay myLocOverlay;
	private boolean isfirstTime;
	private double lat;
	private double lon;

	public static GeoPoint getPoint(double lat, double lon) {
		return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onCreate(Bundle icicle) {
		try {
			super.onCreate(icicle);
			setContentView(R.layout.map_pin);
			map = (MapView) findViewById(R.id.map);
			lat = getIntent().getDoubleExtra(KEY_LAT, 0);
			lon = getIntent().getDoubleExtra(KEY_LON, 0);
			isfirstTime = true;
			map.getController().setZoom(17);
			map.setBuiltInZoomControls(true);
			initMyLocation();
			Drawable marker = getResources().getDrawable(R.drawable.marker);

			marker.setBounds(0, 0, marker.getIntrinsicWidth(),
					marker.getIntrinsicHeight());

			String text = "Latitude:" + lat + "\n" + "Longitude:" + lon;

			map.getOverlays().add(new DriverOverlay(marker, text, lat, lon));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initMyLocation() {
		try {
			myLocOverlay = new MyLocationOverlay(this, map);
			myLocOverlay.enableMyLocation();
			map.getOverlays().add(myLocOverlay);
			myLocOverlay.runOnFirstFix(new Runnable() {
				public void run() {
					try {
						if (isfirstTime) {
							isfirstTime = false;
							final GeoPoint gp = myLocOverlay.getMyLocation();
							final Location location = getLocation(gp);
							map.getController()
									.animateTo(
											getPoint(
													(location.getLatitude() + lat) / 2,
													(location.getLongitude() + lon) / 2),
											new Runnable() {

												public void run() {
													GeoPoint gPoint = getPoint(
															lat, lon);
													map.getController()
															.zoomToSpan(
																	Math.abs(gPoint
																			.getLatitudeE6()
																			- gp.getLatitudeE6()),
																	Math.abs(gPoint
																			.getLongitudeE6()
																			- gp.getLongitudeE6()));

												}

											});
						}
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Location getLocation(GeoPoint gPoint) {
		Location l = new Location(LocationManager.NETWORK_PROVIDER);
		l.setLatitude(gPoint.getLatitudeE6() / 1000000.0);
		l.setLongitude(gPoint.getLongitudeE6() / 1000000.0);
		return l;
	}

	private class DriverOverlay extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> items = new ArrayList<OverlayItem>();
		private Drawable marker = null;
		private String text;
		private double lat;
		private double lon;

		public DriverOverlay(Drawable marker, String text, double lat,
				double lon) {
			super(marker);
			this.text = text;
			this.marker = marker;
			this.lat = lat;
			this.lon = lon;
			items.add(new OverlayItem(getPoint(lat, lon), "You",
					"Your location."));
			populate();
		}

		public double getLat() {
			return lat;
		}

		public double getLon() {
			return lon;
		}

		public String getText() {
			return text;
		}

		@Override
		protected OverlayItem createItem(int i) {
			return (items.get(i));
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

			boundCenterBottom(marker);
		}

		@Override
		protected boolean onTap(int i) {
			// driverTapped(this);
			return (true);
		}

		@Override
		public int size() {
			return (items.size());
		}
	}

}
